/**
 * 系统平台
 * moonvip_admin
 * com.tz.web.cashapply
 * CashApplyController.java
 * 创建人:小胡
 * 时间：2017年01月24日 09:23:34
 *
 */
package com.tz.web.cashapply;

import com.tz.auth.AuthPassport;
import com.tz.bean.CashApply;
import com.tz.bean.TzParams;
import com.tz.service.cashapply.ICashApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.tz.util.TzConstant.SESSION_USER_USERNAME;

/**
 * 提现申请管理web
 * CashApplyController
 * 创建人:小胡
 * 时间：2017年01月24日 09:23:34
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/admin/wallet/cashapply")
public class CashApplyController {

	@Autowired
	private ICashApplyService cashapplyService;

	/*列表查询*/
	@AuthPassport
	@RequestMapping("/list")
	public String list(TzParams params){
		return "wallet/cashapply/list";
	}

	/*模板页面*/
	@RequestMapping("/template")
	public String template(TzParams params ,Model model){
		model.addAttribute("datas", cashapplyService.findCashApplys(params));
		model.addAttribute("itemCount", cashapplyService.count(params));
		return "wallet/cashapply/template";
	}

	@ResponseBody
	@RequestMapping(value="/update",method= RequestMethod.POST)
	public String update(CashApply cashapply){
		cashapplyService.update(cashapply);
		return "success";
	}


	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(TzParams params){
		cashapplyService.delete(params);
		return "success";
	}
	@RequestMapping("/add")
	public String add(TzParams params){
		return "wallet/cashapply/add";
	}


	@RequestMapping(value = "/edit/{id}" ,method = RequestMethod.GET)
	public String edit(@PathVariable("id") Integer id , Model model){
		HashMap<String ,Object> cashapply = cashapplyService.getCashApply(id);
		model.addAttribute("cashapply",cashapply);
		return "wallet/cashapply/edit";
	}


	/**
	 * 提现申请通过
	 */
	//查询前台用户的余额
	//判断是否提现金额小于余额，如果是则提现成功，如果否，则提现失败，告知不能通过
	//如果满足以上条件，则①修改更新提现记录表的处理状态和处理人
	//②更新虚拟账户金额，减去对应的提现金额
	//③新增虚拟账户明细表记录
	//④调用接口。企业付款到个人用户的零钱（需要查询用户的openid）
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/updateAgree",method = RequestMethod.POST )
	public Map<String,Object> updateAgree(CashApply cashapply, HttpServletRequest request){
		String backUsername = (String) request.getSession().getAttribute(SESSION_USER_USERNAME);
		return  cashapplyService.updateAgree(cashapply.getId(), cashapply.getUserId(), cashapply.getMoney(), backUsername,request);
	}

	/**
	 * 提现申请拒绝
	 */
	@AuthPassport
	@ResponseBody
	@RequestMapping("/updateRefuse")
	public int updateRefuse(CashApply cashapply, HttpServletRequest request){
		String backUsername = (String) request.getSession().getAttribute(SESSION_USER_USERNAME);
		cashapply.setDealcode("003");
		cashapply.setOpname(backUsername);
		int n =  cashapplyService.update(cashapply);
		return n;
	}
}
